package com.deer.wms.system.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.system.manage.dao.RoleMapper;
import com.deer.wms.system.manage.model.permission.Permission;
import com.deer.wms.system.manage.model.permission.PermissionModify;
import com.deer.wms.system.manage.model.role.*;
import com.deer.wms.system.manage.service.PermissionService;
import com.deer.wms.system.manage.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author guo
 * @since 2018/8/11
 */
@Service
@Transactional
public class RoleServiceImpl extends SuperServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionService permissionService;


    @Override
    public List<RoleListVO> findRoleFormList(RoleCriteria criteria) {
        List<RoleListVO> voList = roleMapper.selectRoleFormList(criteria);
        return voList;
    }

    @Override
    public RoleDetailVO findRoleByRoleId(Integer roleId) {
        RoleDetailVO vo = roleMapper.selectRoleByRoleId(roleId);
        vo.setTreeList(permissionService.findPermissionTreeFormListByRoleId(roleId));
        return vo;
    }

    @Override
    public void addRole(RoleCreate create, CurrentUser currentUser) {
        //修改角色信息
        Role role = new Role();
        BeanUtils.copyProperties(create, role);
        role.setCreateUserId(currentUser.getUserId());
        // mybatis-plus的自动填充功能日期保存-不能用Date类型
        role.setCreateTime(LocalDateTime.now());
//        Integer companyId = create.getCompanyId();
//        if(companyId==null){
//            role.setCompanyId(currentUser.getCompanyId());
//        }
        super.save(role);
        permissions(role.getRoleId(), create.getPermissions());
    }

    @Override
    public void modifyRole(RoleModify modify, CurrentUser currentUser) {
        //修改角色信息
        Role role = new Role();
        BeanUtils.copyProperties(modify, role);
        // modifyUserId修改人
        role.setModifyUserId(currentUser.getUserId());
        // 修改角色的修改人
        super.updateById(role);
        //处理角色权限信息，为了简单，先删除角色所有权限信息，再新增角色权限
        if (modify.getPermissions() != null) {
            permissionService.deleteByRoleId(modify.getRoleId());
            permissions(role.getRoleId(), modify.getPermissions());
        }
    }

    @Override
    public List<Role> findByRoleName(String roleName) {
        return roleMapper.findByRoleName(roleName);
    }

    @Override
    public Role findBy(String field, String value) {
        QueryWrapper<Role> qw = new QueryWrapper<>();
        return getOne(qw.eq(field, value));
    }

    private void permissions(Integer roleId, List<PermissionModify> permissions) {
        if (permissions == null) {
            return;
        } else if (permissions.size() == 0) {
            return;
        }

        permissions.forEach(data -> {
            Permission permission = new Permission();
            BeanUtils.copyProperties(data, permission);
            if (null != roleId) {
                // 设置角色Id--外键
                permission.setObjectId(roleId);
            }
            permissionService.save2(permission);
        });
    }
}
